package DAOJPA;

import java.util.List;

import film.Participer;
import recompenses.RecompenserFilm;
import sun.reflect.ReflectionFactory.GetReflectionFactoryAction;
import DAO.DAO;
import DAO.DAORecompenserFilm;

public class DAORecompenserFilmJPA extends DAO implements DAORecompenserFilm {

    static private DAORecompenserFilmJPA instance = null;
	
	private DAORecompenserFilmJPA() {
	}
		static public DAORecompenserFilm getInstance() {
		 	if (instance == null)
				instance = new DAORecompenserFilmJPA();
			 	
		 	return instance;
	}
	
	public List<RecompenserFilm> getRecompenserFilm(int codeFilm)
	{
		return DAO.getManager().createQuery("SELECT a FROM RecompenserFilm a WHERE a.id.code_film =:code", RecompenserFilm.class).setParameter("code", codeFilm).getResultList();

	}
		
	@Override
	public RecompenserFilm getRecompenserFilm(int code, int code2) {
		// TODO Auto-generated method stub
		return DAO.getManager().createQuery("SELECT a FROM RecompenserFilm a WHERE a.id.code_film =:code AND a.id.code_rec =:code2 ", RecompenserFilm.class).setParameter("code", code).setParameter("code2", code2).getSingleResult();
		
	}

	@Override
	public List<RecompenserFilm> getRecompenseInvalide() {
		// TODO Auto-generated method stub
		return DAO.getManager().createQuery("SELECT a FROM RecompenserFilm a WHERE a.valide =:code ", RecompenserFilm.class).setParameter("code", 1).getResultList();
		
	}
	
	@Override
	public List<RecompenserFilm> loadAll() {
		// TODO Auto-generated method stub
		return DAO.getManager().createQuery("SELECT a FROM RecompenserFilm a", RecompenserFilm.class).getResultList();

	}

	@Override
	public int getNombreRecompenserFilm() {
		// TODO Auto-generated method stub
		return this.loadAll().size();
	}

	@Override
	public void save(RecompenserFilm a) {
		// TODO Auto-generated method stub
		DAO.getManager().persist(a);
		DAO.commit();
	}

	@Override
	public void delete(RecompenserFilm a) {
		// TODO Auto-generated method stub
		if(a.getFilm().getCode_film() == -1 && a.getRecompense().getCode_rec() != -1) throw new IllegalArgumentException();
		getManager().remove(a);
		DAO.commit();
	}

}
